package org.demo.hbase.demo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class Demo {

	public static Connection connection;

	static {
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "192.168.20.59");
//		conf.set("hbase.zookeeper.property.clientPort", "2181");
		try {
			connection = ConnectionFactory.createConnection(conf);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
//		createTableFamily("test:zxy", "abc");
//		addRowData("test:zxy","1002","abc","name","123456");
		getAllRows("test:zxy");
	}

	// 创建表空间
	public static void createNameSpaces(String nameSpaces) throws Exception {
		Admin admin = connection.getAdmin();
		admin.createNamespace(NamespaceDescriptor.create(nameSpaces).build());
	}

	// 判断表是否存在
	public static boolean isTableExist(String tableName) throws Exception {
		Admin admin = connection.getAdmin();
		return admin.tableExists(TableName.valueOf(tableName));
	}

	// 创建表和列族
	public static void createTable(String tableName, String... columnFamily) throws Exception {
		Admin admin = connection.getAdmin();
		if (isTableExist(tableName)) {
			System.out.println("表" + tableName + "已存在");
		} else {
			// 创建表属性对象,表名需要转字节
			HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
			// 创建多个列族
			for (String cf : columnFamily) {
				descriptor.addFamily(new HColumnDescriptor(cf));
			}
			// 根据对表的配置，创建表
			admin.createTable(descriptor);
			System.out.println("表" + tableName + "创建成功！");
		}
	}

	// 创建列族
	public static void createTableFamily(String tableName, String familyName) throws Exception {
		Admin admin = connection.getAdmin();
		TableName tablename = TableName.valueOf(tableName);
		admin.disableTable(tablename);
		HTableDescriptor hDescriptor = admin.getTableDescriptor(tablename);
		HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(familyName);
		hDescriptor.addFamily(hColumnDescriptor);
		admin.modifyTable(tablename, hDescriptor);
		admin.enableTable(tablename);
	}

	// 添加数据
	public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value)
			throws Exception {
		Table table = connection.getTable(TableName.valueOf(tableName));
		// 向表中插入数据
		Put put = new Put(Bytes.toBytes(rowKey));
		// 向Put对象中组装数据
		put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
		table.put(put);
		System.out.println("插入数据成功");
	}

	// 获取所有的数据
	public static void getAllRows(String tableName) throws Exception {
		Table table = connection.getTable(TableName.valueOf(tableName));
		Scan scan = new Scan();
//		scan.addFamily(Bytes.toBytes("abc"));
		ResultScanner resultScanner = table.getScanner(scan);
		for (Result result : resultScanner) {
			Cell[] cells = result.rawCells();
			for (Cell cell : cells) {
				System.out.println(" 行 键 :" + Bytes.toString(CellUtil.cloneRow(cell)));
				System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell)));
				System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));
				System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));
			}
		}
	}

	// 获取某一行数据
	public static void getRow(String tableName, String rowKey) throws Exception {
		Table table = connection.getTable(TableName.valueOf(tableName));
		Get get = new Get(Bytes.toBytes(rowKey));
		Result result = table.get(get);
		for (Cell cell : result.rawCells()) {
			System.out.println("行键:" + Bytes.toString(result.getRow()));
			System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
			System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
			System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
			System.out.println("时间戳:" + cell.getTimestamp());
		}
	}

	// 获取某一行指定“列族:列”的数据
	public static void getRowQualifier(String tableName, String rowKey, String family, String qualifier)
			throws Exception {
		Table table = connection.getTable(TableName.valueOf(tableName));
		Get get = new Get(Bytes.toBytes(rowKey));
		get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
		Result result = table.get(get);
		for (Cell cell : result.rawCells()) {
			System.out.println("行键:" + Bytes.toString(result.getRow()));
			System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
			System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
			System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
		}
	}

}
